package com.lanou.myapp.book.everydayreading;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.myapp.R;
import com.lanou.myapp.progressdialog.Dialog;

/**
 * Created by dllo on 16/1/15.
 */
public class Readingfragment extends Fragment {
    private String url = "http://api.meiriyiwen.com/v2/day?date=20160116&version=4";
    private Context context;
    private RequestQueue queue;
    private EveryDayRead data;
    private TextView tv_name, tv_author, tv_title;
    private Dialog dialog;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        tv_name = (TextView) getActivity().findViewById(R.id.tv_name_every_day_read);
        tv_author = (TextView) getActivity().findViewById(R.id.tv_author_every_day_read);
        tv_title = (TextView) getActivity().findViewById(R.id.tv_title_every_day_read);
        dialog = new Dialog(context,"");
        dialog.setTitle("正在加载，请稍后...");
        dialog.show();


        queue = Volley.newRequestQueue(getActivity());

        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                EveryDayRead everyDayRead = gson.fromJson(response, EveryDayRead.class);
                data = everyDayRead;
                tv_name.setText(data.getTitle());
                tv_author.setText(data.getAuthor());
                tv_title.setText(data.getContent());
                dialog.dismiss();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_every_day_read_book, null);
        return view;
    }
}
